package cn.bluech.example.ui

import android.annotation.SuppressLint
import android.app.ProgressDialog
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import cn.bluech.example.R
import cn.bluech.example.WifiUtils
import kotlinx.android.synthetic.main.activity_wifi_connect2.*

/**
 * Description:
 * Create by Codeant on 2019/4/16.
 */
class WifiConnectActivity2 : BaseActivity(), View.OnClickListener {
    private var mUtils: WifiUtils? = null

    // wifi之ssid
    private var ssid: String? = null
    private var pwd: String? = null
    private var progressdlg: ProgressDialog? = null

    @SuppressLint("HandlerLeak")
    private val mHandler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                0 -> {
                    showToast("WIFI连接成功")
                    finish()
                }
                1 -> showToast("WIFI连接失败")
            }
            progressDismiss()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wifi_connect2)
        mUtils = WifiUtils(this)
        setLiteners()
        initDatas()
    }

    /**
     * init dialog
     */
    private fun progressDialog() {
        progressdlg = ProgressDialog(this)
        progressdlg!!.setCanceledOnTouchOutside(false)
        progressdlg!!.setCancelable(false)
        progressdlg!!.setProgressStyle(ProgressDialog.STYLE_SPINNER)
        progressdlg!!.setMessage("连接中...")
        progressdlg!!.show()
    }

    /**
     * dissmiss dialog
     */
    private fun progressDismiss() {
        if (progressdlg != null) {
            progressdlg!!.dismiss()
        }
    }

    private fun initDatas() {
        ssid = "codeant";//getIntent().getStringExtra("ssid");
        if (!TextUtils.isEmpty(ssid)) {
            ssid = ssid!!.replace("\"", "")
        }
        ssidTv!!.text = ssid
    }

    private fun setLiteners() {
        connectBtn.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        if (v.id == R.id.connectBtn) { // 下一步操作
            pwd = pwdTv!!.text.toString()
            // 判断密码输入情况
            if (TextUtils.isEmpty(pwd)) {
                Toast.makeText(this, "请输入wifi密码", Toast.LENGTH_SHORT).show()
                return
            }
            progressDialog()
            // 在子线程中处理各种业务
            dealWithConnect(ssid, pwd!!)
        }
    }

    private fun dealWithConnect(ssid: String?, pwd: String) {
        Thread(Runnable {
            Looper.prepare()
            // 检验密码输入是否正确
            val pwdSucess = mUtils!!.connectWifiTest(ssid, pwd)
            try {
                Thread.sleep(4000)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            if (pwdSucess) {
                mHandler.sendEmptyMessage(0)
            } else {
                mHandler.sendEmptyMessage(1)
            }
        }).start()
    }

    private fun showToast(str: String) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show()
    }
}